package com.uzai.console.service.sys.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.uzai.common.enums.MerConfigKeyEnum;
import com.uzai.common.enums.MerConfigTypeEnum;
import com.uzai.common.enums.RedisCacheKeyEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.sys.sensitiveword.SensitiveWordDto;
import com.uzai.console.entity.MerBaseConfig;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.service.sys.SensitiveWordService;
import com.uzai.console.vo.sys.sensitiveword.SensitiveWordVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 敏感词设置
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class SensitiveWordServiceImpl implements SensitiveWordService {

    Logger logger = LoggerFactory.getLogger(SensitiveWordServiceImpl.class);

    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询敏感词设置详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public SensitiveWordVo findSensitiveWordInfo(Long merId){

        SensitiveWordVo sensitiveWordVo = new SensitiveWordVo();

        //查询运营商敏感词设置信息
        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(merId, MerConfigKeyEnum.SENSITIVE_WORD_CONFIG.getValue());
        if(merBaseConfig != null){
            List<String> sensitiveWordList = JSONObject.parseArray(merBaseConfig.getVal(), String.class);
            sensitiveWordVo.setSensitiveWordList(sensitiveWordList);
        }
        return sensitiveWordVo;
    }

    /**
     * 修改敏感词设置详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param sensitiveWordDto
     * @return null
     */
    public void updateSensitiveWord(SensitiveWordDto sensitiveWordDto){

        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(sensitiveWordDto.getMerId(), MerConfigKeyEnum.SENSITIVE_WORD_CONFIG.getValue());

        if(sensitiveWordDto.getSensitiveWordList() != null && sensitiveWordDto.getSensitiveWordList().size() > 50){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "允许最大设置数量为50个");
        }

        Integer now = DateUtil.getNowTime();
        //修改
        if (merBaseConfig != null){
            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(sensitiveWordDto.getSensitiveWordList())));
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.update(merBaseConfig);
        } else { //增加
            merBaseConfig = new MerBaseConfig();
            merBaseConfig.setId(IdWorker.getId());
            merBaseConfig.setMerId(sensitiveWordDto.getMerId());
            merBaseConfig.setType(MerConfigTypeEnum.PRODUCTS.getValue());
            merBaseConfig.setKey(MerConfigKeyEnum.SENSITIVE_WORD_CONFIG.getValue());
            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(sensitiveWordDto.getSensitiveWordList())));
            merBaseConfig.setCreateTime(now);
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.insert(merBaseConfig);
        }

        //保存到Redis当中
        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.SENSITIVE_WORD_CONFIG.getKey(), sensitiveWordDto.getMerId()), Tools.getStrEmpty(JSONObject.toJSONString(sensitiveWordDto.getSensitiveWordList())));
    }

}
